/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.ts.service;

import com.fzhucc.modules.ts.domain.TsTeamCons;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.ts.repository.TsTeamConsRepository;
import com.fzhucc.modules.ts.domain.dto.TsTeamConsDto;
import com.fzhucc.modules.ts.domain.dto.TsTeamConsQueryCriteria;
import com.fzhucc.modules.ts.service.mapstruct.TsTeamConsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-12-07
**/
@Service
@RequiredArgsConstructor
public class TsTeamConsService {

    private final TsTeamConsRepository tsTeamConsRepository;
    private final TsTeamConsMapper tsTeamConsMapper;

    public Map<String,Object> queryAll(TsTeamConsQueryCriteria criteria, Pageable pageable){
        Page<TsTeamCons> page = tsTeamConsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tsTeamConsMapper::toDto));
    }

    public List<TsTeamConsDto> queryAll(TsTeamConsQueryCriteria criteria){
        return tsTeamConsMapper.toDto(tsTeamConsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public TsTeamConsDto findById(Integer id) {
        TsTeamCons tsTeamCons = tsTeamConsRepository.findById(id).orElseGet(TsTeamCons::new);
        ValidationUtil.isNull(tsTeamCons.getId(),"TsTeamCons","id",id);
        return tsTeamConsMapper.toDto(tsTeamCons);
    }

    @Transactional(rollbackFor = Exception.class)
    public TsTeamConsDto create(TsTeamCons resources) {
        return tsTeamConsMapper.toDto(tsTeamConsRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(TsTeamCons resources) {
        TsTeamCons tsTeamCons = tsTeamConsRepository.findById(resources.getId()).orElseGet(TsTeamCons::new);
        ValidationUtil.isNull( tsTeamCons.getId(),"TsTeamCons","id",resources.getId());
        tsTeamCons.copy(resources);
        tsTeamConsRepository.save(tsTeamCons);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tsTeamConsRepository.deleteById(id);
        }
    }

    public void download(List<TsTeamConsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TsTeamConsDto tsTeamCons : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("tb_ts_team 表     id", tsTeamCons.getTsId());
            map.put("姓名", tsTeamCons.getName());
            map.put("学历", tsTeamCons.getEducation());
            map.put("职称", tsTeamCons.getTitle());
            map.put("是否劳模", tsTeamCons.getIsModel());
            map.put("专长", tsTeamCons.getExpertise());
            map.put("联系方式", tsTeamCons.getPhone());
            map.put("专业", tsTeamCons.getSpeciality());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}